package gogen

import (
	_ "embed"
	"strings"

	"github.com/zeromicro/go-zero/tools/goctl/api/spec"
)

//go:embed middleware.tpl
var middlewareImplementCode string

func genMiddleware(dir string, api *spec.ApiSpec) error {
	middlewares := getMiddleware(api)
	var err error
	for _, item := range middlewares {

		filename := strings.ToLower(item[:1]) + item[1:]

		err = genFile(fileGenConfig{
			dir:             dir,
			subdir:          middlewareDir,
			filename:        filename + ".go",
			templateName:    "contextTemplate",
			category:        category,
			templateFile:    middlewareImplementCodeFile,
			builtinTemplate: middlewareImplementCode,
			data: map[string]string{
				"name": item,
			},
		})
		if err != nil {
			return err
		}
	}

	return nil
}

func getRouterMiddleware(dir, name string) error {
	filename := strings.ToLower(name)
	return genFile(fileGenConfig{
		dir:             dir,
		subdir:          middlewareDir,
		filename:        filename + ".go",
		templateName:    "contextTemplate",
		category:        category,
		templateFile:    middlewareImplementCodeFile,
		builtinTemplate: middlewareImplementCode,
		data: map[string]string{
			"name": name,
		},
	})
}
